package com.bootdo.wx.controller;

import java.util.*;

import cn.hutool.core.date.DateUtil;
import com.bootdo.wx.domain.MemberApplyDO;
import com.bootdo.wx.service.MemberApplyService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bootdo.wx.domain.OrderTeamMoneyDO;
import com.bootdo.wx.service.OrderTeamMoneyService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.UuidUtil;
import com.bootdo.common.utils.StringUtils;
/**
 * 团队业绩表
 * 
 * @author long
 * @email 499626561@qq.com
 * @date 2021-05-12 10:17:05
 */
 
@Controller
@RequestMapping("/wx/orderTeamMoney")
public class OrderTeamMoneyController {
	@Autowired
	private OrderTeamMoneyService orderTeamMoneyService;
	@Autowired
	private MemberApplyService memberApplyService;

	@GetMapping()
	@RequiresPermissions("wx:orderTeamMoney:orderTeamMoney")
	String OrderTeamMoney(){
	    return "wx/orderTeamMoney/orderTeamMoney";
	}


	@GetMapping("/{id}/{num}")
	@RequiresPermissions("wx:orderTeamMoney:orderTeamMoney")
	String OrderTeamMoney(@PathVariable ("id") String id, @PathVariable("num") int num, Model model){
		if( num == 1){	//如果参数带1 携带的id就是applyId,如果是2就是代理人Id查询业绩详情
			model.addAttribute("applyId",id);
		}else if(num == 2){
			model.addAttribute("userId",id);
		}

		return "wx/orderTeamMoney/orderTeamMoney";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("wx:orderTeamMoney:orderTeamMoney")
	public PageUtils list(@RequestParam Map<String, Object> params){
	    params.put("delFlag","0");
	    params.put("sort","ca_at");
	    params.put("order","desc");
        //查询列表数据
        Query query = new Query(params);
	    if(params.containsKey("name")&&StringUtils.isNotBlank(params.get("name").toString())){
            List<OrderTeamMoneyDO> orderTeamMoneyList = orderTeamMoneyService.likeList(query);
            int total = orderTeamMoneyList.size();
            PageUtils pageUtils = new PageUtils(orderTeamMoneyList, total);
            return pageUtils;
		}

		if(params.containsKey("applyId")&&StringUtils.isNotBlank(params.get("applyId").toString())){
			List<Map<String,Object>> list =  orderTeamMoneyService.listByApplyId(query);
			int total = orderTeamMoneyService.listByApplyIdTotal(query);
			PageUtils pageUtils = new PageUtils(list,total);
			return pageUtils;
		}else if(params.containsKey("userId")&&StringUtils.isNotBlank(params.get("userId").toString())){
			List<Map<String,Object>> list =  orderTeamMoneyService.listByUserId(query);
			int total = orderTeamMoneyService.listByUserIdTotal(query);
			PageUtils pageUtils = new PageUtils(list,total);
			return pageUtils;
		}

		List<OrderTeamMoneyDO> orderTeamMoneyList = orderTeamMoneyService.list(query);
		int total = orderTeamMoneyService.count(query);
		PageUtils pageUtils = new PageUtils(orderTeamMoneyList, total);
		return pageUtils;
	}
	
	@GetMapping("/add")
	@RequiresPermissions("wx:orderTeamMoney:add")
	String add(){
	    return "wx/orderTeamMoney/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("wx:orderTeamMoney:edit")
	String edit(@PathVariable("id") String id,Model model){
		OrderTeamMoneyDO orderTeamMoney = orderTeamMoneyService.get(id);
		model.addAttribute("orderTeamMoney", orderTeamMoney);
	    return "wx/orderTeamMoney/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("wx:orderTeamMoney:add")
	public R save( OrderTeamMoneyDO orderTeamMoney){
	    orderTeamMoney.setId(UuidUtil.get32UUID());
	    orderTeamMoney.setDelFlag(0);
	    orderTeamMoney.setCaAt(new Date());
		if(orderTeamMoneyService.save(orderTeamMoney)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("wx:orderTeamMoney:edit")
	public R update( OrderTeamMoneyDO orderTeamMoney){
		orderTeamMoney.setUpAt(new Date());
		orderTeamMoneyService.update(orderTeamMoney);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("wx:orderTeamMoney:remove")
	public R remove( String id){
		OrderTeamMoneyDO orderTeamMoney = orderTeamMoneyService.get(id);
		orderTeamMoney.setDelFlag(1);
		orderTeamMoney.setUpAt(new Date());
		if(orderTeamMoneyService.update(orderTeamMoney)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("wx:orderTeamMoney:batchRemove")
	public R remove(@RequestParam("ids[]") String[] ids){
	   if(ids.length>0){
			for (String string : ids) {
				OrderTeamMoneyDO orderTeamMoney = orderTeamMoneyService.get(string);
				orderTeamMoney.setDelFlag(1);
				orderTeamMoney.setUpAt(new Date());
				orderTeamMoneyService.update(orderTeamMoney);
			}
		}
	
		return R.ok();
	}
	
}
